package com.cloudwise.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudwise.dao.MenuDao;
import com.cloudwise.entity.LayUiTree;
import com.cloudwise.entity.Menu;
import com.cloudwise.service.MenuService;
import com.cloudwise.util.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author
 * @since 2022/1/18 15:15:36
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements MenuService {

    @Autowired
    private  MenuDao menuDao;
    @Override
    public List<LayUiTree> queryAllMenusByUserId(Integer userId) {
        //根据用户id查询所有的menus对象
        List<Menu> menuList = menuDao.queryAllMenusByUserId(userId);
        //将menuList转换成layUiTreeList, 0代表顶级菜单，或者是根节点
        List<LayUiTree> layUiTreeList = TreeUtil.getTreeListfromMenuList(menuList, 0);
        return layUiTreeList;
    }

    @Override
    public Set<String> queryAllPermsByUserId(Integer userId) {
        //根据用户id查询所有的menus对象
        List<Menu> menuList = menuDao.queryAllMenusByUserId(userId);
        Set<String> perms=new HashSet<>();
        for (Menu menu : menuList) {
            String menuPerms = menu.getPerms();
            if(null!=menuPerms && menuPerms.length()>0){
                perms.add(menuPerms);
            }
        }
        return perms;
    }
}

